const express = require('express');
const multer = require('multer');
const path = require('path');

const app = express();

// 设置文件存储路径和文件名
const storage = multer.diskStorage({
    destination: (req, file, cb) => {
        cb(null, '../static/'); // 文件存储的目录
    },
    filename: (req, file, cb) => {
        cb(null, file.originalname)
    }
});

// 创建上传实例
const upload = multer({ storage: storage });

// 创建文件上传接口
app.post('/upload', upload.single('file'), (req, res) => {
    try {
        // 上传成功，返回文件信息
        res.status(200).json({
            message: 'File uploaded successfully',
            file: req.file
        });
    } catch (err) {
        res.status(500).json({
            message: 'File upload failed',
            error: err.message
        });
    }
});

app.listen(3000, () => {
    console.log(`这里是upload接口，端口是3000`);
});
